package com.xzqwjw.taskmanager.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leaderinfo.model.CategoryUnit;
import com.leaderinfo.model.UnitNode;
import com.leaderinfo.leaderinfojxyz.R;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UnitCategoryDao implements com.leaderinfo.service.OperateDatabaseService {

	private SQLiteDatabase objDB;
	private String strDBFile = "";

	public UnitCategoryDao(String strDBFile) {
		this.strDBFile = strDBFile;
	}

	@Override
	public Map<String, Object> getRecordByAID(int intAID) {
		CategoryUnit objUnitCategory = null;
		Map<String, Object> mapInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM category_unit WHERE aid=?";
		String[] aryArgs = { String.valueOf(intAID) };
		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.moveToFirst()) {
			objUnitCategory = new CategoryUnit();
			getInfo(objCursor, objUnitCategory);
			mapInfo = new HashMap<String, Object>();
			mapInfo.put("UnitCategory", objUnitCategory);
		}
		objCursor.close();
		objDB.close();
		return mapInfo;
	}

	@Override
	public Map<String, Object> getRecordByBasicID(int intAID) {
		return null;
	}

	@Override
	public List<Map<String, Object>> getRecordsByBasicID(int intAID) {
		return null;
	}

	// ���ݵ�λ��ŵõ��˵�λ������
	public String getUnitNameByID(int intAID) {
		CategoryUnit objUnitCategory = null;
		Map<String, Object> mapInfo = getRecordByAID(intAID);
		if (mapInfo != null) {
			objUnitCategory = (CategoryUnit) mapInfo.get("UnitCategory");
			return objUnitCategory.UnitName;
		} else {
			return "";
		}
	}

	@Override
	public List<Map<String, Object>> getAllRecords() {
		List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapData = null;
		CategoryUnit objUnitCategory = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM category_unit ORDER BY aid";
		Cursor objCursor = objDB.rawQuery(strSQL, null);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapData = new HashMap<String, Object>();
				objUnitCategory = new CategoryUnit();
				getInfo(objCursor, objUnitCategory);
				mapData.put("UnitCategory", objUnitCategory);
				listData.add(mapData);
			}
		}
		objCursor.close();
		objDB.close();
		return listData;
	}

	public List<Map<String, Object>> getRecordsByParentID(int intParentID) {
		List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapData = null;
		CategoryUnit objUnitCategory = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM category_unit WHERE parentid=? AND unitlevel<>'�ɼ�'";
		String[] aryArgs = { String.valueOf(intParentID) };
		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapData = new HashMap<String, Object>();
				objUnitCategory = new CategoryUnit();
				getInfo(objCursor, objUnitCategory);
				mapData.put("UnitCategory", objUnitCategory);
				listData.add(mapData);
			}
		}
		objCursor.close();
		objDB.close();
		return listData;
	}

	private void getInfo(Cursor objCursor, CategoryUnit objUnitCategory) {
		objUnitCategory.AID = objCursor.getInt(objCursor.getColumnIndex("aid"));
		objUnitCategory.UnitName = objCursor.getString(objCursor
				.getColumnIndex("showname"));
		objUnitCategory.ParentID = objCursor.getInt(objCursor
				.getColumnIndex("parentid"));
		objUnitCategory.Depth = objCursor.getInt(objCursor
				.getColumnIndex("depth"));
		objUnitCategory.UnitLevel = objCursor.getString(objCursor
				.getColumnIndex("unitlevel"));
		objUnitCategory.IsLegalPerson = objCursor.getString(objCursor
				.getColumnIndex("islegalperson"));
		objUnitCategory.PrincipalNumber = objCursor.getInt(objCursor
				.getColumnIndex("principalnumber"));
		objUnitCategory.DeputyNumber = objCursor.getInt(objCursor
				.getColumnIndex("deputynumber"));
	}

	public static void addNode(UnitNode nodeRoot, UnitNode nodeParent,
			int intParentID, String strDBFile) {
		UnitNode objNode;
		CategoryUnit objUnitCategory;
		Map<String, Object> mapData;
		List<Map<String, Object>> listData = null;
		com.leaderinfo.dao.UnitCategoryDao objUnitCategoryDAO = new com.leaderinfo.dao.UnitCategoryDao(strDBFile);

		listData = objUnitCategoryDAO.getRecordsByParentID(intParentID);

		if (listData != null) {
			for (int i = 0; i < listData.size(); i++) {
				mapData = listData.get(i);
				objUnitCategory = (CategoryUnit) mapData.get("UnitCategory");
				if (mapData != null) {
					if (intParentID == 0) {
						objNode = new UnitNode(objUnitCategory.UnitName,
								objUnitCategory.AID + "");
						objNode.setParent(nodeRoot);
						objNode.setIcon(R.drawable.ic_folder);
						objNode.setCheckBox(false);
						nodeRoot.add(objNode);
						intParentID = objUnitCategory.AID;
						// �ݹ����
						addNode(nodeRoot, objNode, intParentID, strDBFile);
					} else {
						objNode = new UnitNode(objUnitCategory.UnitName,
								objUnitCategory.AID + "");
						objNode.setParent(nodeParent);
						objNode.setIcon(R.drawable.ic_folder);
						objNode.setCheckBox(false);
						nodeParent.add(objNode);
						intParentID = objUnitCategory.AID;
						// �ݹ����
						addNode(nodeRoot, objNode, intParentID, strDBFile);
					}
				}
			}
		}

	}

	@Override
	public List<Map<String, Object>> getRecordsByWhere(String strWhere) {
		return null;
	}

}
